package utils;

import java.io.*;
import java.util.*;

public class ConfigReaderSlave extends Object {

    private static Hashtable configTable = null;
    private static String fs = File.separator;
    private static String folderConfig = System.getProperty("user.dir") + fs + "BGATEPOKER" + fs + "config" + fs;
    private static String folderLog = System.getProperty("user.dir") + fs + "BGATEPOKER" + fs + "loginfo" + fs;
    private static String fileNameCfg;
    private static String pathFileConfig;
    private static int platform;

    private void DetectOS() {
        String osName = System.getProperty("os.name");
        osName = osName.toUpperCase();
        if (osName.compareTo("LINUX") == 0) {
            platform = Def.OS_UNIX;
        } else {
            platform = Def.OS_WINDOWS;
        }
    }

    /**
     * Creates new ConfigReader with default path
     */
    public ConfigReaderSlave() {
        DetectOS();
//        if (platform == Def.OS_UNIX) {
//            pathFileConfig = pathFileConfigUnix;
//            pathFileLog = pathFileLogUnix;
//        }
        fileNameCfg = "config.cfg";
        pathFileConfig = folderConfig;
        System.out.println("path file config:" + pathFileConfig);
        init();
    }

    private void init() {
        BufferedReader bf;
        configTable = new Hashtable();
        try {
            bf = new BufferedReader(new InputStreamReader(new FileInputStream(pathFileConfig + fileNameCfg), "UTF-8"));
            String str;
            while ((str = bf.readLine()) != null) {
                if (str.length() > 2) {
                    //GAP CAC cau co // thi bo qua : chu thich
                    if (str.charAt(0) == '/' && str.charAt(1) == '/') {
                        continue;
                    }
                    int pos = str.indexOf('=');
                    //[DATABASE] thi cung bo qua
                    if (pos <= 0) {
                        continue;
                    }
                    String key = str.substring(0, pos).trim();
                    // Debug.d("key:" + key);
                    String value = str.substring(pos + 1).trim();
                    // Debug.d("value:" + value);
                    configTable.put(key, value);
                }
            }
            bf.close();
        } catch (FileNotFoundException ex) {//HungDM : khong co file config thi phai tao moi mot file default, roi dua vao bang TableConfig
            try {
                boolean result = true;
                if (pathFileConfig.length() > 1) {
                    File folder = new File(pathFileConfig);
                    if (!folder.exists()) {
                        result = folder.mkdirs() || folder.mkdir();
                        Debug.d("make folder");

                    }
                    Debug.d("exist :" + folder.exists());
                }
                Debug.d("result :" + result);
                if (result) {
                    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(pathFileConfig, fileNameCfg))));
                    pw.println("[GENERAL]");
                    pw.println(Def.LOGFILE_LOCATION + " = " + folderLog);
                    pw.println("[DATABASE]");
                    pw.println(Def.STR_DB_TYPE + " = " + Def.STR_DB_MYSQL);
                    pw.println(Def.STR_DB_IPADDR + " = 202.92.4.22");
                    pw.println(Def.STR_DB_PORT + " = 3306");
                    pw.println(Def.STR_DB_SID + " = poker_online");
                    pw.println(Def.STR_DB_USERNAME + " = gamepoker");
                    pw.println(Def.STR_DB_PASSWORD + " = GaMe2o12");
                    pw.println("[SLAVE]");
                    pw.println(Def.STR_SLAVE_IPADDR + " = 202.92.4.22");
                    pw.println(Def.STR_SLAVE_PORT + " = 6666");
                    pw.println(Def.STR_SLAVE_ID + " = 1");

                    pw.println(Def.NUMBER_ROBOT + " = 100");
                    pw.println(Def.NUMBER_ROBOT_PER_TABLE + " = 2");
                    pw.flush();
                    pw.close();
                    init();//goi lai ham nay de load cac thang tin vao bang config

                } else {
                    System.out.println("Unable to create folder for config");
                    return;
                }
            } catch (IOException e) {
                System.out.println("Create Error: " + e.toString());
                return;
            }
        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            return;
        }
    }

    public synchronized String getValue(String key) {
        if (configTable == null) {
            init();
        }
        String value = null;
        value = (String) configTable.get(key);
        return value;
    }

    public synchronized static boolean changeValue(String key, String value) {
        if (configTable == null || key == null || value == null
                || key.length() == 0 || value.length() == 0) {
            return false;
        }
        configTable.remove(key);
        configTable.put(key, value);
        return true;
    }

    public synchronized static boolean changeValue(String key, boolean value) {
        if (value) {
            return changeValue(key, "1");
        } else {
            return changeValue(key, "0");
        }
    }

    public synchronized Hashtable getProperties() {
        if (configTable == null) {
            init();
        }
        return configTable;
    }

    public static void main(String[] args) {
//        ConfigReaderSlave cf = new ConfigReaderSlave();
//        System.out.println("Begin");
//        System.out.println("[GENERAL]");
//        System.out.println("[DATABASE]");
//        System.out.println("DB_IPADDR = " + ConfigReaderSlave.getValue(Def.STR_DB_IPADDR));
//        System.out.println("DB_PORT = " + ConfigReaderSlave.getValue(Def.STR_DB_PORT));
//        System.out.println("DB_SID = " + ConfigReaderSlave.getValue(Def.STR_DB_SID));
//        System.out.println("DB_USERNAME = " + ConfigReaderSlave.getValue(Def.STR_DB_USERNAME));
//        System.out.println("DB_PASSWORD = " + ConfigReaderSlave.getValue(Def.STR_DB_PASSWORD));
//        System.out.println("SEVER_IPADDR = " + ConfigReaderSlave.getValue(Def.STR_SLAVE_IPADDR));
//        System.out.println("SEVER_PORT = " + ConfigReaderSlave.getValue(Def.STR_SLAVE_PORT));
//        System.out.println("NUMBER_ROBOT = " + ConfigReaderSlave.getValue(Def.NUMBER_ROBOT));
//        System.out.println("NUMBER_ROBOT_PER_TABLE = " + ConfigReaderSlave.getValue(Def.NUMBER_ROBOT_PER_TABLE));
//        System.out.println("End");
    }
}
